<?php

$rows = array();

$rows[] = array('按部门');
$tmp = array('月分');
foreach($months as $month){ $tmp[] ="{$month}月"; }
$tmp[] = '累计';
$tmp[] = '平均';
$rows[] = $tmp;
foreach($data['dept'] as $dept_id=>$dept){
	$tmp = array($dept['name']);
	foreach($months as $month){ $tmp[] = fnum($dept['month'][$month]); }
	$tmp[] = fnum($dept['total']);
	if(count($dept['month'])){
		$tmp[] = fnum($dept['total']/count($dept['month']));
	}
	$rows[] = $tmp;
}
//		<td>合计</td>
//		str_repeat('<td></td>', 13)
//		<td>/</td>

$rows[] = array();
$rows[] = array('按供应商');
$tmp = array('月分');
foreach($months as $month){ $tmp[] ="{$month}月"; }
$tmp[] = '累计';
$tmp[] = '平均';
if($suppliers) foreach($suppliers as $supplier){
	$tmp = array($supplier['name']);
	foreach($months as $month){ $tmp[] = fnum($data['supplier'][$supplier['id']]['month'][$month]); }
	$tmp[] = fnum($data['supplier'][$supplier['id']]['total']);
	if(count($data['supplier'][$supplier['id']]['month'])){
		$tmp[] = fnum($data['supplier'][$supplier['id']]['total'] / count($data['supplier'][$supplier['id']]['month']) );
	}
        if($data['supplier'][$supplier['id']]['total']>0)
        {
             $rows[] = $tmp; 
        }       	
}
//		<td>合计</td>
//		str_repeat('<td></td>', 13)
//		<td>/</td>

$rows[] = array();
$rows[] = array('按类型');
$tmp = array('月分');
foreach($months as $month){ $tmp[] ="{$month}月"; }
$tmp[] = '累计';
$tmp[] = '平均';
if($suppliers) foreach(array('补库采购','专项采购','紧急采购') as $typeid=>$type){
	$tmp = array($type);
	foreach($months as $month){ $tmp[] = fnum($data['type'][$typeid]['month'][$month]); }
	$tmp[] = fnum($data['type'][$typeid]['total']);
	if(count($data['type'][$typeid]['month'])){
		$tmp[] = fnum($data['type'][$typeid]['total'] / count($data['type'][$typeid]['month']) );
	}
	$rows[] = $tmp;
}
//		<td>合计</td>
//		str_repeat('<td></td>', 13)
//		<td>/</td>

Exporter::csv($rows, (fetch_a_int('year')?:date('Y')).'年度采购汇总');

?>
